﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Nacos.AspNetCore.V2;
using Nacos.V2.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyWeb.ServiceBus.Extensions.Nacos
{
    public static class NacosSetup
    {
        public static void AddNacosSetup(this IServiceCollection services, IConfigurationRoot? config)
        {
            if (services == null) throw new ArgumentNullException(nameof(services));


            NacosConfig? nacosConfig = new NacosConfig();
            config.Bind("Nacos:config", nacosConfig);

            if (!(nacosConfig is null))
            {
                // 安装 nacos-sdk-csharp
                services.AddNacosV2Config(x =>
                {
                    x.ServerAddresses = nacosConfig.ServerAddresses;
                    x.EndPoint = "";
                    x.Namespace = nacosConfig.Namespace;
                    x.DefaultTimeOut = nacosConfig.DefaultTimeOut;
                    x.ListenInterval = nacosConfig.ListenInterval;
                    /*x.UserName = "nacos";
                   x.Password = "nacos";*/
                    // swich to use http or rpc
                    x.ConfigUseRpc = true;//是否使用最新的配置  ConfigUseRpc 和 NamingUseRpc，分别表示配置和服务是否要使用最新的 gRPC 协议和服务端对接。
                });

                services.AddNacosV2Naming(x =>
                {
                    x.ServerAddresses = nacosConfig.ServerAddresses;
                    x.EndPoint = "";
                    x.Namespace = nacosConfig.Namespace;
                    x.DefaultTimeOut = nacosConfig.DefaultTimeOut;
                    x.ListenInterval = nacosConfig.ListenInterval;
                    /*x.UserName = "nacos";
                   x.Password = "nacos";*/
                    // swich to use http or rpc
                    x.NamingUseRpc = true;//是否使用最新的服务
                });

                // 安装 nacos-sdk-csharp.AspNetCore
                // 注册服务到Nacos
                services.AddNacosAspNet(x =>
                {
                    x.ServerAddresses = nacosConfig.ServerAddresses;
                    x.Namespace = nacosConfig.Namespace;
                    x.DefaultTimeOut = nacosConfig.DefaultTimeOut;
                    x.ListenInterval = nacosConfig.ListenInterval;
                    //x.UserName = "nacos";
                    //x.Password = "nacos";
                    x.GroupName = "NET";
                    x.ServiceName = nacosConfig.ServiceName;
                    x.Port = nacosConfig.Port;
                    x.Weight = 1;//设置权重
                });
            }
        }
    }
    public class NacosConfig
    {
        /// <summary>
        /// 服务地址
        /// </summary>
        public List<string>? ServerAddresses { get; set; }
        /// <summary>
        /// 默认超时事件
        /// </summary>
        public int DefaultTimeOut { get; set; }
        /// <summary>
        /// 命名空间
        /// </summary>
        public string Namespace { get; set; } = string.Empty;
        /// <summary>
        /// 监听的频率
        /// </summary>
        public int ListenInterval { get; set; }
        /// <summary>
        /// 服务名
        /// </summary>
        public string ServiceName { get; set; } = string.Empty;
        /// <summary>
        /// 服务端口号
        /// </summary>
        public int Port { get; set; }
        /// <summary>
        /// 是否直接注册nacos
        /// </summary>
        public bool RegisterEnabled { get; set; }

    }
}
